package cn.net.awit.API.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecgframework.codegenerate.extcommon.a;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.util.PasswordUtil;
import org.jeecgframework.web.system.pojo.base.TSUser;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.web.system.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**   
 * @Title: Controller  
 * @Description: 移动端登录
 * @author onlineGenerator
 * @date 2018-10-29 09:33:37
 * @version V1.0   
 *
 */
@Controller
@RequestMapping("/login")
public class APILoginController extends BaseController {
	
	private static final Logger logger = LoggerFactory.getLogger(APILoginController.class);

	@Autowired
	private UserService userService;
	@Autowired
	private SystemService systemService;
	
	/**
	 * 登录操作
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params = "doLogin",method=RequestMethod.POST)
	@ResponseBody
	public AjaxJson doLogin(HttpServletRequest request,HttpServletResponse response) throws Exception{
		AjaxJson ajaxJson = new AjaxJson();
		
		try {
			String username = request.getParameter("userName");
			String password = request.getParameter("passWord");
			if(username==null || "".equals(username)|| password==null || "".equals(password)){
				ajaxJson.setSuccess(false);
				ajaxJson.setMsg("用户名或密码为空。");
				return ajaxJson;
			}
			TSUser user = new TSUser();
			user.setUserName(username);
			user.setPassword(password);
			TSUser userExits = userService.checkUserExits(user);

			if (userExits==null) {	//用户名和密码登录验证失败
				ajaxJson.setSuccess(false);
				ajaxJson.setMsg("用户不存在或密码错误。");
				return ajaxJson;
			}
			if (userExits != null && userExits.getStatus() != 0) {	//用户名和密码验证成功且被激活
				ajaxJson.setSuccess(true);
				ajaxJson.setMsg("登陆成功！！！");
				Map<String, Object> map = new HashMap();
				map.put("userName", userExits.getUserName());
				map.put("realName", userExits.getRealName());
				map.put("mobilPhone", userExits.getMobilePhone());
				map.put("email", userExits.getEmail());
				map.put("departName", userExits.getCurrentDepart().getDepartname());
				ajaxJson.setObj(map);
				
			}else{
				ajaxJson.setSuccess(false);
				ajaxJson.setMsg("用户未被激活,请联系管理员！！！");
			}
		} catch (Exception e) {
			ajaxJson.setSuccess(false);
			ajaxJson.setMsg("失败:code:"+e.getMessage());
			throw e;
		}
		
		return ajaxJson ;
	}
	
}
